#include <QtGui>

#include "glwidget.h"
#include "window.h"

Window::Window()
{
    glWidget = new GLWidget;
	glWidget->setWindowPointer(this);

	innerSlider = createIntSlider(1, 64);
	outerSlider = createIntSlider(1, 64);
	nIterSlider = createIntSlider(1, 64);
	stepSizeSlider = createIntSlider(1, 100);
	epsSlider	   = createIntSlider(1, 20);
	funcScaleSlider= createIntSlider(1, 50);

	innerLabel = new QLabel("in   ", 0, 0);
	outerLabel = new QLabel("out   ");
	nIterLabel = new QLabel("iter   ");
	stepSizeLabel = new QLabel("step   ");
	epsLabel	  = new QLabel("eps        ");
	funcScaleLabel= new QLabel(tr("scal   "), 0, 0);


	xSlider = createSlider();
	ySlider = createSlider();
	zSlider = createSlider();
	//xSlider->setValue(15 * 16);
	//ySlider->setValue(345 * 16);
	//zSlider->setValue(0 * 16);

	subWire = new QCheckBox("subwire");
	subWire->setChecked(glWidget->GetNeedGS());	

	InnOut = new QCheckBox("Inner&Outer");
	InnOut->setChecked(glWidget->GetInnerOuter());	


    connect(xSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setXRotation(int)));
    connect(glWidget, SIGNAL(xRotationChanged(int)), xSlider, SLOT(setValue(int)));
    connect(ySlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setYRotation(int)));
    connect(glWidget, SIGNAL(yRotationChanged(int)), ySlider, SLOT(setValue(int)));
    connect(zSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setZRotation(int)));
    connect(glWidget, SIGNAL(zRotationChanged(int)), zSlider, SLOT(setValue(int)));

	connect(innerSlider, SIGNAL(valueChanged(int)), glWidget,   SLOT(setTessInner(int)));
	connect(innerSlider, SIGNAL(valueChanged(int)),	this,	SLOT(setTessInnerLabel(int)));
	connect(outerSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setTessOuter(int)));
	connect(outerSlider, SIGNAL(valueChanged(int)),	this,	SLOT(setTessOuterLabel(int)));

	connect(nIterSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setIterNum(int)));
	connect(nIterSlider, SIGNAL(valueChanged(int)),	this,	SLOT(setIterLabel(int)));
	connect(stepSizeSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setStepsize(int)));
	connect(stepSizeSlider, SIGNAL(valueChanged(int)),	this,	SLOT(setStepsizeLabel(int)));
	connect(epsSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setEps(int)));
	connect(epsSlider, SIGNAL(valueChanged(int)),	this,	SLOT(setEpsLabel(int)));
	connect(funcScaleSlider, SIGNAL(valueChanged(int)), glWidget, SLOT(setFuncScale(int)));
	connect(funcScaleSlider, SIGNAL(valueChanged(int)),	this,	SLOT(setFuncScaleLabel(int)));

	connect(subWire, SIGNAL(toggled(bool)), glWidget, SLOT(setSubWire(bool)));
	connect(InnOut, SIGNAL(toggled(bool)), glWidget, SLOT(setInnOutFlag(bool)));
	connect(glWidget, SIGNAL(setInnOut(int)), innerSlider, SLOT(setValue(int)));
	connect(glWidget, SIGNAL(setInnOut(int)), outerSlider, SLOT(setValue(int)));
	connect(glWidget, SIGNAL(setInnOut(int)), innerLabel, SLOT(setValue(int)));
	connect(glWidget, SIGNAL(setInnOut(int)), innerLabel, SLOT(setValue(int)));


	QGridLayout *ctrLayout = new QGridLayout;
	int rInd=0;
	ctrLayout->addWidget(InnOut, rInd++, 0, 1, 2);
	ctrLayout->addWidget(innerLabel, rInd, 0);
	ctrLayout->addWidget(innerSlider, rInd++, 1);
	ctrLayout->addWidget(outerLabel, rInd, 0);
	ctrLayout->addWidget(outerSlider, rInd++, 1);
	ctrLayout->addWidget(nIterLabel, rInd, 0);
	ctrLayout->addWidget(nIterSlider, rInd++, 1);
	ctrLayout->addWidget(stepSizeLabel, rInd, 0);
	ctrLayout->addWidget(stepSizeSlider, rInd++, 1);
	ctrLayout->addWidget(epsLabel, rInd, 0);
	ctrLayout->addWidget(epsSlider, rInd++, 1);
	ctrLayout->addWidget(funcScaleLabel, rInd, 0);
	ctrLayout->addWidget(funcScaleSlider, rInd++, 1);
	ctrLayout->addWidget(subWire, rInd++, 0);
	ctrLayout->setRowStretch(rInd++,20);
	ctrLayout->addWidget(xSlider, rInd++, 0, 1, 2);
	ctrLayout->addWidget(ySlider, rInd++, 0, 1, 2);
	ctrLayout->addWidget(zSlider, rInd++, 0, 1, 2);


    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(glWidget);	
	mainLayout->addLayout(ctrLayout);
    setLayout(mainLayout);

    setWindowTitle(tr("Hello GL"));
}
void Window::setFuncScaleLabel(int par)
{
	QString text_float;
	text_float.setNum(par*0.1);
	funcScaleLabel->setText("scal "+text_float);	
}
void Window::setEpsLabel(int par)
{
	QString text_float = QString::number(pow(10.0,-(par*.5)), 'g', 2 );
	for(int tsize = text_float.size(); tsize<7; tsize++)
			text_float.append(' ');
	epsLabel->setText("eps "+text_float);		
}
void Window::setStepsizeLabel(int par)
{
	QString text_float;
	text_float.setNum(par*0.02);
	stepSizeLabel->setText("size "+text_float);	
}

void Window::setTessInnerLabel(int inner)
{
	QString text_int;
	text_int.setNum(inner);
	innerLabel->setText("in "+text_int);	
}

void Window::setTessOuterLabel(int outer)
{
	QString text_int;
	text_int.setNum(outer);
	outerLabel->setText("out "+text_int);
}

void Window::setIterLabel(int iter)
{
	QString text_int;
	text_int.setNum(iter);
	nIterLabel->setText("iter "+text_int);

}
QSlider *Window::createSlider()
{
    QSlider *slider = new QSlider(Qt::Horizontal);
    slider->setRange(0, 360 * 16);
    slider->setSingleStep(16);
    slider->setPageStep(15 * 16);
    slider->setTickInterval(15 * 16);
    slider->setTickPosition(QSlider::TicksRight);
    return slider;
}

QSlider *Window::createIntSlider(int minN, int maxN)
{
    QSlider *slider = new QSlider(Qt::Horizontal);
    slider->setRange(minN, maxN);
    //slider->setTickInterval(1);
    //slider->setTickPosition(QSlider::TicksRight);
    return slider;
}

void Window::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Escape)
        close();
    else
        QWidget::keyPressEvent(e);
}
